package resources.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.servlet.ModelAndView;

import resources.dao.RoleDAO;
import resources.dao.UserDAO;

import resources.domain.Role;
import resources.domain.User;

import resources.service.RoleService;

/**
 * Spring MVC controller that handles CRUD requests for Role entities
 * 
 */

@Controller("RoleController")
public class RoleController {

	/**
	 * DAO injected by Spring that manages Role entities
	 * 
	 */
	@Autowired
	private RoleDAO roleDAO;

	/**
	 * DAO injected by Spring that manages User entities
	 * 
	 */
	@Autowired
	private UserDAO userDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for Role entities
	 * 
	 */
	@Autowired
	private RoleService roleService;

	/**
	 * Select an existing Role entity
	 * 
	 */
	@RequestMapping("/selectRole")
	public ModelAndView selectRole(@RequestParam Integer idKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("role", roleDAO.findRoleByPrimaryKey(idKey));
		mav.setViewName("role/viewRole.jsp");

		return mav;
	}

	/**
	 * Delete an existing User entity
	 * 
	 */
	@RequestMapping("/deleteRoleUsers")
	public ModelAndView deleteRoleUsers(@RequestParam Integer role_id, @RequestParam Integer related_users_id) {
		ModelAndView mav = new ModelAndView();

		Role role = roleService.deleteRoleUsers(role_id, related_users_id);

		mav.addObject("role_id", role_id);
		mav.addObject("role", role);
		mav.setViewName("role/viewRole.jsp");

		return mav;
	}

	/**
	 * Show all Role entities
	 * 
	 */
	@RequestMapping("/indexRole")
	public ModelAndView listRoles() {
		ModelAndView mav = new ModelAndView();

		mav.addObject("roles", roleService.loadRoles());

		mav.setViewName("role/listRoles.jsp");

		return mav;
	}

	/**
	 * Edit an existing Role entity
	 * 
	 */
	@RequestMapping("/editRole")
	public ModelAndView editRole(@RequestParam Integer idKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("role", roleDAO.findRoleByPrimaryKey(idKey));
		mav.setViewName("role/editRole.jsp");

		return mav;
	}

	/**
	 * View an existing User entity
	 * 
	 */
	@RequestMapping("/selectRoleUsers")
	public ModelAndView selectRoleUsers(@RequestParam Integer role_id, @RequestParam Integer users_id) {
		User user = userDAO.findUserByPrimaryKey(users_id, -1, -1);

		ModelAndView mav = new ModelAndView();
		mav.addObject("role_id", role_id);
		mav.addObject("user", user);
		mav.setViewName("role/users/viewUsers.jsp");

		return mav;
	}

	/**
	 * Create a new User entity
	 * 
	 */
	@RequestMapping("/newRoleUsers")
	public ModelAndView newRoleUsers(@RequestParam Integer role_id) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("role_id", role_id);
		mav.addObject("user", new User());
		mav.addObject("newFlag", true);
		mav.setViewName("role/users/editUsers.jsp");

		return mav;
	}

	/**
	 * Create a new Role entity
	 * 
	 */
	@RequestMapping("/newRole")
	public ModelAndView newRole() {
		ModelAndView mav = new ModelAndView();

		mav.addObject("role", new Role());
		mav.addObject("newFlag", true);
		mav.setViewName("role/editRole.jsp");

		return mav;
	}

	/**
	 * Delete an existing Role entity
	 * 
	 */
	@RequestMapping("/deleteRole")
	public String deleteRole(@RequestParam Integer idKey) {
		Role role = roleDAO.findRoleByPrimaryKey(idKey);
		roleService.deleteRole(role);
		return "forward:/indexRole";
	}

	/**
	 * Register custom, context-specific property editors
	 * 
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	 * Entry point to show all Role entities
	 * 
	 */
	public String indexRole() {
		return "redirect:/indexRole";
	}

	/**
	 * Edit an existing User entity
	 * 
	 */
	@RequestMapping("/editRoleUsers")
	public ModelAndView editRoleUsers(@RequestParam Integer role_id, @RequestParam Integer users_id) {
		User user = userDAO.findUserByPrimaryKey(users_id, -1, -1);

		ModelAndView mav = new ModelAndView();
		mav.addObject("role_id", role_id);
		mav.addObject("user", user);
		mav.setViewName("role/users/editUsers.jsp");

		return mav;
	}

	/**
	 * Show all User entities by Role
	 * 
	 */
	@RequestMapping("/listRoleUsers")
	public ModelAndView listRoleUsers(@RequestParam Integer idKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("role", roleDAO.findRoleByPrimaryKey(idKey));
		mav.setViewName("role/users/listUsers.jsp");

		return mav;
	}

	/**
	 * Save an existing User entity
	 * 
	 */
	@RequestMapping("/saveRoleUsers")
	public ModelAndView saveRoleUsers(@RequestParam Integer role_id, @ModelAttribute User users) {
		Role parent_role = roleService.saveRoleUsers(role_id, users);

		ModelAndView mav = new ModelAndView();
		mav.addObject("role_id", role_id);
		mav.addObject("role", parent_role);
		mav.setViewName("role/viewRole.jsp");

		return mav;
	}

	/**
	 * Select the child User entity for display allowing the user to confirm that they would like to delete the entity
	 * 
	 */
	@RequestMapping("/confirmDeleteRoleUsers")
	public ModelAndView confirmDeleteRoleUsers(@RequestParam Integer role_id, @RequestParam Integer related_users_id) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("user", userDAO.findUserByPrimaryKey(related_users_id));
		mav.addObject("role_id", role_id);
		mav.setViewName("role/users/deleteUsers.jsp");

		return mav;
	}

	/**
	 * Select the Role entity for display allowing the user to confirm that they would like to delete the entity
	 * 
	 */
	@RequestMapping("/confirmDeleteRole")
	public ModelAndView confirmDeleteRole(@RequestParam Integer idKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("role", roleDAO.findRoleByPrimaryKey(idKey));
		mav.setViewName("role/deleteRole.jsp");

		return mav;
	}

	/**
	 * Save an existing Role entity
	 * 
	 */
	@RequestMapping("/saveRole")
	public String saveRole(@ModelAttribute Role role) {
		roleService.saveRole(role);
		return "forward:/indexRole";
	}

	/**
	 */
	@RequestMapping("/roleController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request, @ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}
}